#include "ctrl.h"

//家的坐标
Axis3f home_point = {
	.x = 0, .y = 0, .z = 0
};

#define height		(30)
#define X1		(0)
#define Y1		(20)

//棋子的坐标
Axis3f get_piece_point[10] = {
	{.x = 0 + 55, .y = 20, .z = height},
	{.x = 0 + 55, .y = 50, .z = height},
	{.x = 0 + 55, .y = 80, .z = height},
	{.x = 0 + 55, .y = 110, .z = height},
	{.x = 0 + 55, .y = 140, .z = height},
	{.x = 167 + 55, .y = 20, .z = height},
	{.x = 167 + 55, .y = 50, .z = height},
	{.x = 167 + 55, .y = 80, .z = height},
	{.x = 167 + 55, .y = 110, .z = height},
	{.x = 167 + 55, .y = 140, .z = height},
};

//放置棋子的坐标
Axis3f play_piece_point[9] = {
	{.x = X1 + 51.5  + 55,    .y = Y1 + 92, .z = height},
	{.x = X1 + 83.5  + 55,    .y = Y1 + 92, .z = height},
	{.x = X1 + 115.5 + 55,    .y = Y1 + 92, .z = height},
	{.x = X1 + 51.5  + 55,    .y = Y1 + 60, .z = height},
	{.x = X1 + 83.5  + 55,    .y = Y1 + 60, .z = height},
	{.x = X1 + 115.5 + 55,    .y = Y1 + 60, .z = height},
	{.x = X1 + 51.5  + 55,    .y = Y1 + 28, .z = height},
	{.x = X1 + 83.5  + 55,    .y = Y1 + 28, .z = height},
	{.x = X1 + 115.5 + 55,    .y = Y1 + 28, .z = height},
};

#define xyz_delay(x)					while (!xyend_GetEndFlag() || !zend_GetEndFlag()) { \
																vTaskDelay(x); \
															}

												
#define taj_delay(tja, x)			while(TRAJ_ModelScale_ForDis(tja) < x) {	\
																vTaskDelay(2); \
															}											
												
/**
 * @brief  起子
 * @param  
 * @retval 无
 */
void get_piece(uint8_t num)
{
	/* 开取 */
	can_motor_setz(get_piece_point[num].z);
	xyz_delay(2);
	
	can_motor_setxy(get_piece_point[num].x, get_piece_point[num].y);
	
	/* 等待取子完成 */
	xyz_delay(2);
	
	/* 吸住 */
	chess_zi_set(1);
	
	vTaskDelay(1);
	
	can_motor_setz(0);
	
	/* 等待吸子完成 */
	xyz_delay(2);
}

/**
 * @brief  放子
 * @param  
 * @retval 无
 */
void set_piece(uint8_t num)
{
	/* 开取 */
	can_motor_setz(get_piece_point[num].z);
	xyz_delay(2);
	
	can_motor_setxy(get_piece_point[num].x, get_piece_point[num].y);
	
	/* 等待跑到取琪区上方 */
	xyz_delay(2);
	
	/* 下降开放 */
	can_motor_setz(4);
	
	taj_delay(Traj_ricez, 0.7f);
	chess_zi_set(0);
	
	xyz_delay(2);
	
//	vTaskDelay(100);
	
	can_motor_setz(height);
	xyz_delay(2);
}

/**
 * @brief  落子
 * @param  
 * @retval 无
 */
void play_piece(uint8_t playnum)
{
	/* 开放 */
	can_motor_setxy_ForPark(play_piece_point[playnum].x, play_piece_point[playnum].y);
	can_motor_setz(play_piece_point[playnum].z);
	
	/* 等待放子完成 */
	xyz_delay(2);
	
	can_motor_setz(0);
	
	/* 是时候放下了 */
	taj_delay(Traj_ricez, 0.7f);
	chess_zi_set(0);
	
	/* 等待吸子完成 */
	xyz_delay(2);
//	taj_delay(Traj_ricez, 0.7f);
	
	vTaskDelay(100);
	
	/* 先上来再回去 */
	can_motor_setz(play_piece_point[playnum].z);
	
	/* 等待吸子完成 */
	xyz_delay(2);
}

/**
* @brief  取棋盘上的棋子
 * @param  
 * @retval 无
 */
void take_piece(uint8_t takenum)
{
	/* 开取 */
	can_motor_setz(play_piece_point[takenum].z);
	xyz_delay(2);
	
	can_motor_setxy_ForPark(play_piece_point[takenum].x, play_piece_point[takenum].y);
	
	/* 打开吸盘 */
	chess_zi_set(1);
	
	/* 停留在棋子上空 */
	xyz_delay(2);
	
	/* 下降开吸 */
	can_motor_setz(4);

	/* 等待吸子完成 */
	xyz_delay(2);
	//taj_delay(Traj_ricez, 0.7f);
	
	vTaskDelay(1);
	
	/* 吸完上升 */
	can_motor_setz(height);
}

/**
 * @brief  回老家
 * @param  
 * @retval 无
 */
void go_home(void)
{
	/* 回家 */
	can_motor_setxy(home_point.x, home_point.y);
	xyz_delay(2);
	
//	taj_delay(Traj_ricexy, 0.98f);
	can_motor_setz(0);
	/* 等待吸子完成 */
	xyz_delay(2);
}

/**
 * @brief  换子
 * @param  
 * @retval 无
 */
void change_piece(uint8_t num, uint8_t new_num)
{
	//拿旧的
	buzz_didi(1);
	take_piece(num);
	
	//放到新的位置
	buzz_didi(1);
	play_piece(new_num);
	
	go_home();
	
	//ok啦
	buzz_didi(2);
}

/**
 * @brief  下棋
 * @param  
 * @retval 无
 */
void playChess(uint8_t num, uint8_t playnum)
{
	buzz_didi(1);
	/* 拿到棋子 */
	get_piece(num);
	
	buzz_didi(1);
	
	/* 放置棋子 */
	play_piece(playnum);
	
	go_home();
	
	buzz_didi(2);
}

/**
 * @brief  放棋
 * @param  
 * @retval 无
 */
void replayChess(uint8_t num, uint8_t playnum)
{	
	buzz_didi(1);
	
	/* 拿到棋子 */
	take_piece(playnum);

	buzz_didi(1);
	
	/* 放置棋子 */
	set_piece(num);
	
	buzz_didi(2);
}

uint8_t qizi[4][2];
/**
 * @brief  执行题目1
 * @param  
 * @retval 无
 */
void run_timu_handmove2(void)
{
	if (!fun_key_get(3)) {
		return;
	}
	buzz_didi(4);
	
	WS_Clear(0);
	

	uint8_t q_z = 0;
	uint32_t who_name;
	
	//获取棋子
	for (uint8_t i=0;i<4;) {
		
		if (data_key_get_bg()) {
			who_name = 0xff0000;
		} else {
			who_name = 0x0000ff;
		}
		led_on(i, who_name);
		
		switch(q_z) {
			/* 取哪个子 */
			case 0:{
				uint8_t num = data_key_get_num();
				
				if (num > 0 && num < 6) {
					qizi[i][0] = (num - 1) + (data_key_get_bg() * chess_zi_num_max);
				} else if (num > 0) {
					buzz_didi(3);
				}
				
				if (fun_key_get(2)) {
					q_z = 1;
				}
				break;
			}
			
			case 1:{
				/* 放在哪里 */
				uint8_t num = data_key_get_num();
				
				if (num) {
					qizi[i][1] = (num - 1);
				}
				
				if (fun_key_get(2)) {
					q_z = 0;
					i++;
				}
				break;
			}
		}
		vTaskDelay(20);
	}
	
	for (uint8_t i=0;i<4;i++) {
//		playChess(qizi[i][0], qizi[i][1]);
		buzz_didi(1);
		/* 拿到棋子 */
		get_piece(qizi[i][0]);
		
		buzz_didi(1);
		
		/* 放置棋子 */
		play_piece(qizi[i][1]);
		
		buzz_didi(2);
	}
	
	go_home();
	
	for (uint8_t i=0; i < 10; i++) {
		vTaskDelay(200);WS_Clear(0x00ff00);
	}
	buzz_didi(2);
	WS_Clear(0);
}

/**
 * @brief  执行题目1
 * @param  
 * @retval 无
 */
void run_timu_handmove(void)
{
	if (!fun_key_get(1)) {
		return;
	}
	buzz_didi(2);
	
	uint8_t chess_zi_num;
	uint32_t who_name;
	
//	led_on(5, who_name);
//	led_on(6, who_name);
//	led_on(7, who_name);
	
	/* 获取取哪个子 */
	for (;;) {
		if (data_key_get_bg()) {
			who_name = 0xff0000;
		} else {
			who_name = 0x0000ff;
		}
		
		WS_Clear(who_name);
	
		uint8_t key = data_key_get_num();
					
		if (key > 0 && key < 6) {
			buzz_didi(1);
			chess_zi_num = (key-1) + data_key_get_bg() * 5;
			break;
		}
		led_on(2, who_name);
		vTaskDelay(20);
	}
				
	/* 下哪里 */
	for (;;) {
		if (fun_key_get(1)) {	//搞个按键确认一下
			buzz_didi(2);
			break;
		}
		vTaskDelay(20);
	}
		
	for (;;) {
			uint8_t key = data_key_get_num();
				
			if (key) {
				buzz_didi(1);
				playChess(chess_zi_num,key-1);
				Close_led();
				return;
			}
			led_on(3, who_name);
			
			vTaskDelay(20);
	}
}

//剩余多少棋子
uint8_t chess_zi_residue_wr;
uint8_t chess_zi_residue_bl;

/**
 * @brief  取子
 * @param  
 * @retval 无
 */
void get_chess_zi(uint8_t mycolor, uint8_t *residue, uint8_t true_take)
{
	uint8_t role = mycolor - 1;
	int8_t num;
		
	int cnt = 1;
	
	/* 获取落子点 */
	if (true_take == 0) {	//是不是真拿
//	if (0) {	
	Close_led();
	while (1) {
		
		//里面有乱拿矫正算法
		num = tactics_key_get_num(mycolor);
			
		//有按键
		if (num != -1) {
			buzz_didi(1);
			break;
		}
		
		if (cnt++ > 15 * 1000 + 1) {
			if (cnt % 1000 == 0) {
				buzz_didi(3);
				for (uint8_t i=0; i < 8; i++) {
					led_not(i,0xffff00);
				}
			}
		} else {
			if (cnt % 1000 == 0) {
				buzz_didi(1);
			}
			
			uint8_t lednum = cnt / 1000;
			
//			for (uint8_t i=0; i < lednum; i++) {
				if (lednum<8) {
					led_on(lednum,0x00ff00);
				} else {
					if (lednum == 8) {
						Close_led();
					}
					led_on(lednum-8,0xff0000);
				}
//			}
		}
		vTaskDelay(1);
	}
	
	Close_led();
	
} else {
	while (1) {
		num = data_key_get_num();
		
		//有按键
		if (num) {
			//按键选中没有人下
			if (chessboard[num-1] == 0) {
				//下
				num = num-1;
				break;
			}
			
			//老六，盖我，警告一次
			buzz_didi(2);
		}
		
		if (cnt++ > 15 * 1000 + 1) {
			if (cnt % 1000 == 0) {
				buzz_didi(1);
			}
		}
		vTaskDelay(1);
	}
	
	//下棋了
	playChess(role*chess_zi_num_max + (chess_zi_num_max-*residue), num);

}

	/* 选择用了什么棋子消失一个 */
	*residue = *residue - 1;
	
	chessboard[num] = role + 1;
}

/**
 * @brief  自动算法
 * @param  
 * @retval 无
 */
void get_chess_zi_formAuto(uint8_t mycolor, uint8_t *residue)
{
	uint8_t role = mycolor - 1;
	uint8_t num;
	
	num = get_black_num(chessboard, mycolor);
	
	//下棋了
	playChess(role*chess_zi_num_max + (chess_zi_num_max-*residue), num);
	
	/* 选择用了什么棋子消失一个 */
	*residue = *residue - 1;
	
	chessboard[num] = role + 1;
}

/**
 * @brief  自动下棋
 * @param  
 * @retval 无
 */
void run_timu_auto_one(uint8_t mycolor, uint8_t youcolor)
{
	/* 黑子先说话 */
	if (mycolor == user_bl) {
		//while(!fun_key_get(3)){vTaskDelay(200);WS_Clear(0x00ff00);};	//卡住等待落子
		get_chess_zi_formAuto(user_bl, &chess_zi_residue_bl);	//我是阿尔法鸭，我是黑子，输入1获取1赢得策略
	} else {
		get_chess_zi(user_bl, &chess_zi_residue_bl, 0);
	}
	
	/* 判断输赢 */
	uint8_t sf = isVictory(chessboard, mycolor);
	uint8_t pj = isDraw(chessboard);
	
	if (sf || pj) {
		return ;
	}
	
	/* 白子唯唯诺诺不敢说话 */
	if (youcolor == user_bl) {
		//while(!fun_key_get(3)){vTaskDelay(200);WS_Clear(0x00ff00);};	//卡住等待落子
		get_chess_zi_formAuto(user_wr, &chess_zi_residue_wr);
	} else {
		//1代表电脑我是小黑子
		get_chess_zi(user_wr, &chess_zi_residue_wr, 0);
	}	
}

void ws_pj(uint8_t num)
{
	if (num == 0) {
		WS_Clear(0xff0000);
		led_on(4, 0x0000ff);
		led_on(5, 0x0000ff);
		led_on(6, 0x0000ff);
		led_on(7, 0x0000ff);
	} else {
		WS_Clear(0x0000ff);
		led_on(4, 0xff0000);
		led_on(5, 0xff0000);
		led_on(6, 0xff0000);
		led_on(7, 0xff0000);
	}
}

/**
* @brief  显示平局 
 * @param  
 * @retval 无
 */
void show_draw(void)
{
	buzz_didi(1);
	ws_pj(0);
	vTaskDelay(500);
	
	buzz_didi(1);
	ws_pj(1);
	vTaskDelay(500);
	
	buzz_didi(1);
	ws_pj(0);
	vTaskDelay(500);
	
	buzz_didi(1);
	ws_pj(1);
	vTaskDelay(500);
}

/**
* @brief  显示输赢
 * @param  
 * @retval 无
 */
void show_Winning(uint8_t sf)
{
	uint32_t color;
	if (sf == 1) {
		color = 0xff0000;
	} else {
		color = 0x0000ff;
	}
	
	buzz_didi(1);
	WS_Clear(color);
	vTaskDelay(500);
	
	buzz_didi(1);
	WS_Clear(0);
	vTaskDelay(500);
	
	buzz_didi(1);
	WS_Clear(color);
	vTaskDelay(500);
	
	buzz_didi(1);
	WS_Clear(0);
	vTaskDelay(500);
	
	buzz_didi(1);
	WS_Clear(color);
	vTaskDelay(500);
	
	buzz_didi(1);
	WS_Clear(0);
	vTaskDelay(500);
	
	buzz_didi(1);
	WS_Clear(color);
	vTaskDelay(500);
	
	buzz_didi(1);
	WS_Clear(0);
	vTaskDelay(500);
}


/**
 * @brief  执行题目
 * @param  
 * @retval 无
 */
void run_timu_auto(void)
{	
	if (!fun_key_get(2)) {
		return;
	}
	
	chess_zi_residue_wr = chess_zi_num_max;
	chess_zi_residue_bl = chess_zi_num_max;
	clern_chess(chessboard);
	
	uint8_t my;
	uint8_t you;
	
	/* 获取哪方 */ 
	if (data_key_get_bg() == 1) {
		my  = user_bl;	//向上你是白方，我是黑子
		you = user_wr;
		
		//指定先下黑子
		get_chess_zi(1, &chess_zi_residue_bl, 1);	//我是小黑子我先下
		get_chess_zi(2, &chess_zi_residue_wr, 0);
	} else {
		my  = user_wr;	//向上你是白方，我是黑子
		you = user_bl;
	}
	
	WS_Clear(0x00ff00);
	buzz_didi(3);
	
	for (;;) {
		run_timu_auto_one(my, you);
		
		//先判断输赢
		uint8_t sf = isVictory(chessboard, my);
		if (sf) {
			show_Winning(sf);
			break;
		}
		
		//平局
		if (isDraw(chessboard)) {
			show_draw();
			break;
		}
		
		vTaskDelay(1);
	}
	
//	while(1) {
//		if (fun_key_get(4)) {
//			break;
//		}
//		vTaskDelay(100);
//	}
	
	/* 复原棋子 */
	
	tja_xy = 1200;
	tja_z  = 1200;
	for (uint8_t i=0; i < 9; i++) {
		if (chessboard[i] != 0) {
			//多下的给他放回去
				if (chessboard[i] == user_wr) {
					replayChess(chess_zi_num_max*2 - chess_zi_residue_wr - 1, i);
					chess_zi_residue_wr++;
				} else {
					replayChess(chess_zi_num_max - (chess_zi_residue_bl) - 1, i);
					chess_zi_residue_bl++;
				}
		}
	}
	tja_xy = 800;
	tja_z  = 1200;
	go_home();
	
	for (uint8_t i=0; i < 10; i++) {
		vTaskDelay(200);WS_Clear(0x00ff00);
	}
	buzz_didi(2);
	WS_Clear(0);
}

/******************* (C) COPYRIGHT 2022 FUYEBING*****END OF FILE************/
